﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;

namespace BDSim
{
    /// <summary>
    /// LoadAstronomyEnvFile.xaml 的交互逻辑
    /// </summary>
    public partial class LoadAstronomyEnvFile : Window
    {
        public LoadAstronomyEnvFile(int type)
        {
            InitializeComponent();
            this.type = type;
            if (type == 1)
            {
                this.Title = "加载行星历表文件";
                label1.Content = "Planets Calendar Datas";
                textBox1.Text = "2012-03-30 00:00:00"; //"2456016.5";
                textBox2.Text = "2029-11-08 00:00:00"; //"2462448.5";
            }
            if (type == 2)
            {
                this.Title = "加载EOP文件";
                label1.Content = "Earth Orientation Parameters";
                textBox1.Text = "2011-01-01 00:00:00";
                textBox2.Text = "2016-06-30 00:00:00";
            }
        }

        //数据定义
        private bool selResult = false;
        public bool SelResult
        {
            get { return selResult; }
            set { selResult = value; }
        }
        private string fileName = "";
        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        private int type = 1;
        private string filePath = "";

        //事件方法
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件(*.txt)|*.txt";
            ofd.InitialDirectory = filePath;
            ofd.FileName = textBox3.Text;

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int index = ofd.FileName.LastIndexOf('\\');
                filePath = ofd.FileName.Substring(0, index + 1);
                textBox3.Text = ofd.FileName.Substring(index + 1);
                textBox3.ToolTip = ofd.FileName;
                show_file_info(ofd.FileName);
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.FileName = filePath + textBox3.Text;
            this.selResult = true;
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.selResult = false;

            int index = FileName.LastIndexOf('\\');
            filePath = FileName.Substring(0, index + 1);
            textBox3.Text = FileName.Substring(index + 1);
            textBox3.ToolTip = FileName;
            show_file_info(FileName);
        }

        private void show_file_info(string file)
        {            
            int ret = 0;
            EopFileInfo info = new EopFileInfo();

            //根据类型type读取相应文件信息
            if (type == 1) {
                ret = BDSim.ReadEphdat.RE_GetEphDatInfo(file.ToCharArray(), out info);
            }
            if (type == 2) {
                ret = BDSim.ReadEOP.EOP_GetEopInfo(file.ToCharArray(), out info);
            }

            //结果判断并显示
            if (ret == 1) {
                textBox1.Text = (new String(info.startTime)).Substring(0, 10) + " 00:00:00";
                textBox2.Text = (new String(info.endTime)).Substring(0, 10) + " 00:00:00";
            }
            else {
                textBox1.Text = "0000-00-00 00:00:00";
                textBox2.Text = "0000-00-00 00:00:00";
                String str = (type == 1) ? "读取行星历表文件信息失败！" : "读取EOP文件信息失败！";
                System.Windows.Forms.MessageBox.Show(str, "错误");
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.selResult = false;
            this.Close();
        }
    }
}
